﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DrillOnline.Services
{
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Spreadsheet;
    using DocumentFormat.OpenXml.Packaging;
    public class DocumentServices
    {
        public class SheetSource {
            public string Title { get; set; }
            public string[] Headers { get; set; }
            public IList<string[]> Rows { get; set; }
        }
        
        public static byte[] CreateSpreadsheetDocument(params SheetSource[] sheetsources)
        {
            //if (titles.Length != data.Count) throw new ArgumentException("列数与取值表达式数不匹配");

            using (var stream = new System.IO.MemoryStream())
            {
                using (var xlDoc = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
                {

                    WorkbookPart bookPart = xlDoc.AddWorkbookPart();
                    Workbook book = new Workbook();
                    bookPart.Workbook = book;

                    Sheets sheets = new Sheets();
                    book.AppendChild<Sheets>(sheets);
                    foreach (var item in sheetsources)
                    {
                        CreateSheet(item.Title, item.Headers, item.Rows, bookPart, sheets);
                    }
                }
                return stream.ToArray();
            }
        }

        private static void CreateSheet(string sheetTitle, string[] titles, IList<string[]> data, WorkbookPart bookPart, Sheets sheets)
        {
            WorksheetPart sheetPart = bookPart.AddNewPart<WorksheetPart>();
            SheetData sd = new SheetData();
            Worksheet worksheet = new Worksheet(sd);
            sheetPart.Worksheet = worksheet;

            Sheet sheet = new Sheet() { Id = bookPart.GetIdOfPart(sheetPart), SheetId = 1001, Name = sheetTitle };
            sheets.Append(sheet);

            var titlecells = new Cell[titles.Length];
            for (int i = 0; i < titles.Length; i++)
            {
                titlecells[i] = new Cell()
                {
                    CellValue = new CellValue(titles[i]),
                    DataType = new EnumValue<CellValues>(CellValues.String)
                };
            }
            sd.Append(new Row(titlecells));


            var rs = new Row[data.Count];
            var dt = new EnumValue<CellValues>(CellValues.String);
            for (int r = 0; r < rs.Length; r++)
            {

                var cs = new Cell[data[r].Length];
                for (int c = 0; c < cs.Length; c++)
                {
                    cs[c] = new Cell()
                    {
                        CellValue = new CellValue(data[r][c]),
                        DataType = dt
                    };
                }
                rs[r] = new Row(cs);
            }
            sd.Append(rs);
        }

    }
}